﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraBars;
using System;
using System.Threading;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统日志管理
    /// </summary>
    [FormCaption("系统日志管理")]
    public partial class FrmLogManager : FrmSysBase
    {
        private GridHelper<SysLog> helper;
        private readonly bool allowSelect = SysRuntime.IsAuthorize("sys.log.select");

        public FrmLogManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            editOperateStartDateTime.Config(SysConst.DEFAULT_DATE_PATTERN, p =>
             {
                 p.Properties.MaxValue = DateTime.Now.Date;
                 p.EditValue = DateTime.Now.Date.AddDays(-7);
             });
            editOperateEndDateTime.Config(SysConst.DEFAULT_DATE_PATTERN, p =>
            {
                p.Properties.MaxValue = DateTime.Now.Date;
                p.EditValue = DateTime.Now.Date;
            });

            //1.初始化表格操作类
            helper = new GridHelper<SysLog>(gridView);
            helper.ElapsedTimeItem = labExecuteTime;

            //2.加载首页数据
            LoadFirstPageData();
            ButtonStatus();
        }

        /// <summary>
        /// 加载第一页数据
        /// </summary>
        private void LoadFirstPageData() => LoadData(1);

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="pageIndex">页码</param>
        private void LoadData(int? pageIndex = null)
        {
            //权限控制
            if (!allowSelect) return;

            helper.SetPageIndex(pageIndex);
            var filter = GetFilterModel();
            var paged = helper.Paged;

            helper.RunCallback(e =>
            {
                e.Result = LogService.Instance.SelectPageList(paged, filter);
            })
            .CompletCallback(e =>
            {
                ButtonStatus();
            })
            .RunAsync();
        }

        /// <summary>
        /// 数据编辑
        /// </summary>
        private void Details()
        {
            var editForm = new FrmLogDetails(helper);
            editForm.ShowDialogOk();
        }

        private LogFilter GetFilterModel()
        {
            LogFilter filter = new LogFilter();
            WinHelper.GetFormData(panelFilter, filter);
            if (filter.OperateEndDateTime != null)
            {
                filter.OperateEndDateTime = filter.OperateEndDateTime.Value.AddDays(1);
            }
            return filter;
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            btnDelete.Visibility = BarItemVisibility.Never;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmLogManager_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        /// <summary>
        /// 表格双击事件
        /// </summary>
        private void gridView_DoubleClick(object sender, EventArgs e)
        {
            helper.DblClickRow(e, Details);
        }

        /// <summary>
        /// 表格选中项发生变化事件
        /// </summary>
        private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            ButtonStatus();
        }

        private void gridView_EndSorting(object sender, EventArgs e)
        {
            //helper.View.BeginDataUpdate();
            //LoadData();
            //helper.View.EndDataUpdate();
        }

        /// <summary>
        /// 页码变化事件
        /// </summary>
        private void gridPager_PageChanged(object sender, PagerEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 搜索按钮点击事件
        /// </summary>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //加载首页数据
            LoadFirstPageData();
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.Delete(() =>
            {
                //LogService.Instance.Delete(helper.SelectedIds).IfTrue(() =>
                //{
                //    gridView.DeleteSelectedRows();
                //    LoadData();
                //});
            });
        }

    }
}